package com.example.module_home.vm

import com.example.lib_common.view.vm.BaseViewModel
import com.example.module_home.api.HomeApiServer
import com.example.module_home.entity.AddDeviceEntity
import com.example.module_home.entity.DeviceEntity
import com.example.module_home.entity.DeviceEntityItem
import com.example.module_home.entity.HouseEntity
import com.example.module_home.model.HomeRepository
import com.example.module_home.model.HomeResultData
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.launch
import javax.inject.Inject

@HiltViewModel
class DeviceViewModel  @Inject constructor(
    private val apiServer: HomeApiServer
):BaseViewModel<HomeRepository,HomeResultData<List<DeviceEntityItem>>>(){

    fun getListData(){
        mViewModelScope.launch(mDispatcher){
            try {
                val resultData = apiServer.getDevice()
                val res = HomeResultData(
                    state = resultData.state,
                    msg = resultData.msg,
                    data = resultData.data
                )
                mSuccessLiveData.postValue(res)
            } catch (e: Exception) {
               mErrorLiveData.postValue("请求失败:${e.message}")
            }
        }
    }

    fun AddDevice(addDeviceEntity: AddDeviceEntity){
        mViewModelScope.launch(mDispatcher){
            try {
                val addDevice = apiServer.getAddDevice(addDeviceEntity)
                val res = HomeResultData(
                    state = addDevice.state,
                    msg = addDevice.msg,
                    data = listOf(addDevice.data)
                )
                mSuccessLiveData.postValue(res)
            } catch (e: Exception) {
                mErrorLiveData.postValue("请求失败: ${e.message}")
            }
        }
    }


}